<?php
namespace Admin\Controller;

class RoleController extends AdminController
{
    public function index()
    {
        $this->display();
    }
    public function view()
    {
        $n = D("Role");
        $node = D("Node");
        import("ORG.Util.Page"); //导入分页类
        $page = $_POST["page"];
		$rows = empty(I("limit"))?1:I("limit");
        $sort = $_POST["sort"];
        $order = $_POST["order"];
        $count = $n->count(); //计算总数
        $list = $n->relation(true)->page($page, $rows) ->order('id desc')->select();
        foreach ($list as $key => $val) {
            $pieces = explode(",", rtrim($val['rules'], ','));
            foreach ($pieces as $k => $v) {
                $v=empty($v)?0:$v;
                $nodelist = $node->where('id=' . $v)->select();
                $list[$key]['nodes'][$k] = $nodelist[0];
                $list[$key]['nodeid['.$nodelist[0]['id'].']']=$nodelist[0]['id'];
            }
        }
        $list = json_encode($list);
        $result = '{"status":1,"msg":"成功啦","total":' . $count . ',"rows":' . $list . '}';
        echo ($result); //输出json数据
    }
    //添加
    public function add()
    {
        if (empty($_POST)) {
            $nodelist=$this->getNodelist();
            $this->assign('nodelist',$nodelist );
            $this->display();
        } else {
            $m = D("Role");
            $_POST['rules']=implode(',',$_POST['nodeid']);
            if ($vo = $m->create()) {
                if ($m->add($vo)) {
                    $this->success('添加用户成功！', 1);
                } else {
                    $this->error('添加失败！', 0);
                }
            } else {
                $this->error($m->getError(), 3);
            }}
    }
    // 更新数据
    public function edit()
    {
        if (empty($_POST)) {
            $nodelist=$this->getNodelist();
            $this->assign('nodelist',$nodelist );
            $role=M('Role')->find($_GET['id']);
            $role['rules']=trim($role['rules'],',');
            $role['rules']=explode(",",$role['rules']);
            // dump($role); 
            $this->assign('role',$role );
            $this->display();
        } else {
            $Form = D("Role");
            $_POST['rules']=empty($_POST['nodeid'])?'':implode(',',$_POST['nodeid']);
            if ($vo = $Form->create()) {
                if ($Form->save($vo)) {
                    $this->success('更新成功！', 1);
                } else {
                    $this->error('没有更新任何数据!', 0);
                }
            } else {
                $this->error($Form->getError(), 0);
			}
		}
    }
    // 删除数据
    public function delete()
    {
        if (!empty($_GET['ids'])) {
            $d = D("Role");
            $condition['id'] = array('in', $_GET['ids']);
            $result = $d->where($condition)->delete();
            if (false !== $result) {
                $ac = M("access");
                $condition1['role_id'] = array('in', $_GET['ids']);
                $ac->where($condition1)->delete();
                $this->success('删除成功！', 1);
            } else {
                $this->error($d->getError(), 0);
            }
        } else {
            $this->error('删除项不存在！' . $d->getError(), 3);
        }
    }
    public function getNodelist(){        
        $nodelist = M('Node')->order('id desc')->select();
        $nodelist = \Org\Util\Tree::toLayer($nodelist);
        return $nodelist;
    }
}
